<template>
  <a-form
    ref="formRef"
    :model="formState"
    :rules="formRules"
    :label-col="labelCol"
    :wrapper-col="wrapperCol"
  >
    <div class="c_table">
      <div class="c_require">
        <a-form-item ref="author" label="作者名" name="name">
          <a-input v-model:value="formState.author" />
        </a-form-item>

        <a-form-item ref="packageName" label="作者名" name="name">
          <a-input v-model:value="formState.packageName" />
        </a-form-item>

        <a-form-item ref="moduleName" label="模块名" name="name">
          <a-input v-model:value="formState.moduleName" />
        </a-form-item>

        
        <a-form-item ref="outPutDir" label="代码输出根目录" name="name">
          <a-input v-model:value="formState.outPutDir" />
        </a-form-item>

        <a-form-item ref="databaseUrl" label="数据库连接" name="name">
          <a-input v-model:value="formState.databaseUrl" />
        </a-form-item>

        <a-form-item ref="userName" label="数据用户" name="name">
          <a-input v-model:value="formState.userName" />
        </a-form-item>

        <a-form-item ref="password" label="数据库密码" name="name">
          <a-input v-model:value="formState.password" />
        </a-form-item>

        <a-form-item ref="includes" label="表" name="name">
          <a-input v-model:value="formState.includes" />
        </a-form-item>
      </div>
      <div class="c_template">
        <a-form-item
          ref="controllerTemplatePath"
          label="控制器模板"
          name="name"
        >
          <a-input v-model:value="formState.controllerTemplatePath" />
        </a-form-item>

        <a-form-item ref="entityTemplatePath" label="实体类模板" name="name">
          <a-input v-model:value="formState.entityTemplatePath" />
        </a-form-item>

        <a-form-item ref="mapperTemplatePath" label="mapper模板" name="name">
          <a-input v-model:value="formState.mapperTemplatePath" />
        </a-form-item>

        <a-form-item
          ref="mapperXmlTemplatePath"
          label="mapperXml模板"
          name="name"
        >
          <a-input v-model:value="formState.mapperXmlTemplatePath" />
        </a-form-item>

        <a-form-item ref="serviceTemplatePath" label="svc模板" name="name">
          <a-input v-model:value="formState.serviceTemplatePath" />
        </a-form-item>

        <a-form-item
          ref="serviceImplTemplatePath"
          label="svc实现模板"
          name="name"
        >
          <a-input v-model:value="formState.serviceImplTemplatePath" />
        </a-form-item>
      </div>
      <div class="c_switch">
        <a-form-item label="是否从sql中生成" name="useSql">
          <a-switch v-model:checked="formState.useSql" />
        </a-form-item>
        <div v-show="formState.useSql">
          <a-form-item ref="sqlFilePath" label="sql目录" name="name" >
            <a-input v-model:value="formState.sqlFilePath" />
          </a-form-item>
        </div>
        <a-form-item label="是否生成controller" name="genController">
          <a-switch v-model:checked="formState.genController" />
        </a-form-item>

        <a-form-item label="是否生成Entity" name="genEntity">
          <a-switch v-model:checked="formState.genEntity" />
        </a-form-item>

        <a-form-item label="是否生成Mapper" name="genMapper">
          <a-switch v-model:checked="formState.genMapper" />
        </a-form-item>

        <a-form-item label="是否生成MapperXml" name="genMapperXml">
          <a-switch v-model:checked="formState.genMapperXml" />
        </a-form-item>

        <a-form-item label="是否生成service" name="genService">
          <a-switch v-model:checked="formState.genService" />
        </a-form-item>

        <a-form-item label="是否生成serviceImpl" name="genServiceImpl">
          <a-switch v-model:checked="formState.genServiceImpl" />
        </a-form-item>
        <a-form-item :wrapper-col="{ span: 14, offset: 4 }">
          <a-button type="primary" @click="onSubmit">Create</a-button>
          <a-button style="margin-left: 10px">Cancel</a-button>
        </a-form-item>
      </div>
      <div class="c_footer"></div>
    </div>
  </a-form>
</template>
<script lang="ts">
import { ValidateErrorEntity } from "ant-design-vue/es/form/interface";
import { Moment, ConfigJsonStruct, ConfigJsonDefault } from "./model/";
import { formRules } from "./model/rule";
import { defineComponent, reactive, ref, toRaw, UnwrapRef,h } from "vue";
import { notification } from "ant-design-vue";
import { CheckCircleOutlined,ExclamationCircleOutlined} from "@ant-design/icons-vue";
import axios from "axios";
interface FormState {
  name: string;
  region: string | undefined;
  date1: Moment | undefined;
  delivery: boolean;
  type: string[];
  resource: string;
  desc: string;
}
export default defineComponent({
  setup() {
    const formRef = ref();
    const formState: UnwrapRef<ConfigJsonStruct> = reactive(ConfigJsonDefault);

    const onSubmit = () => {
      formRef.value
        .validate()
        .then(() => {
            console.log(toRaw(formState));
            
        })
        .catch((error: ValidateErrorEntity<FormState>) => {
          console.log("error", error);
        });
        const response= axios.post("/codeGen", toRaw(formState));
        axios
            .post("/codeGen", toRaw(formState))
            .then((response) => {
              alert("创建成功")
              // notification.success({
              //   message: "创建成功",
              //   description:"创建成功",
              //   duration: 0,
              //   // icon: h(CheckCircleOutlined, { style: "color: green" }),
              // });
              // console.log(response);
            })
            .catch((error) => {
              alert("创建失败,"+error)
              // notification.error({
              //   message: "错误提示",
              //   description:error,
              //   duration: 0,
              //   // icon: h(ExclamationCircleOutlined,{style: "color:red"})
              // });
              // console.log(error);
            });
    };
    const resetForm = () => {
      formRef.value.resetFields();
    };
    return {
      formRef,
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      other: "",
      formState,
      formRules,
      onSubmit,
      resetForm,
    };
  },
});
</script>
<style>
.c_table {
  margin-top: 50px;
  /* margin-bottom: 50px; */
}
.c_require {
}
.c_template {
}
.c_switch {
  /* margin-bottom: 50px; */
}
.c_footer {
  display: block;
  width: 100%;
  height: 50px;
}
</style>
